<?php


namespace app\admin\controller;
use app\common\model\SystemLog;
use think\facade\Db;


// 管理组
class MgGroup extends Base
{
    /**
     * 管理组列表
     * date 2020/8/6 23:22
     */
    public function group_list() {
        $list = Db::name('mg_group')
            ->where('mg_module', $this->mg_module)
            ->field('id, parent_id, group_name, access, status, update_time')
            ->select();
        $list = arr_tree($list);
        foreach ($list as &$item) {
            $item['group_name']   = str_repeat('&nbsp;', ($item['level'] - 1) * 8) . '|-' . $item['group_name'];
            $item['group_people'] = Db::name('mg_member')
                ->where('group_id', $item['id'])
                ->count();
        }
        $this->assign('list', $list);
        return $this->fetch('common@mg_group/group_list');
    }

    /**
     * 添加管理组
     * date 2020/8/6 23:25
     */
    public function add_group() {
        if(request()->post()) {
            $data = $_POST;
            $data['mg_module'] = $this->mg_module;
            $data['access'] = implode(',', $data['access']);
            $data['create_time'] = time();
            $data['update_time'] = time();
            Db::name('mg_group')->insert($data) ? json_response(1, '保存成功') : json_response(0, '保存失败');
        }else {
            $group_list = Db::name('mg_group')
                ->where('mg_module', $this->mg_module)
                ->field('id, parent_id, group_name')
                ->select();
            $group_list = arr_tree($group_list);
            foreach ($group_list as &$item) {
                $item['group_name']   = str_repeat('&nbsp;', ($item['level'] - 1) * 8) . '|-' . $item['group_name'];
            }
            $access_list = Db::name('mg_menu')
                ->where('mg_module', $this->mg_module)
                ->field('id, parent_id, name')
                ->order('sort ASC, id DESC')
                ->select();
            $access_list = arr_tree($access_list, true);
            $this->assign('group_list', $group_list);
            $this->assign('access_list', $access_list);
            $this->assign('data', ['access'=>[]]);
            return $this->fetch('common@mg_group/add_group');
        }
    }

    /**
     * 编辑管理组
     * date 2020/8/6 23:26
     */
    public function edit_group() {
        $id = input('id');
        if(request()->isPost()) {
            $data = $_POST;
            $data['access'] = implode(',', $data['access']);
            $data['update_time'] = time();
            $res = Db::name('mg_group')
                ->where(['id'=>$id, 'mg_module'=>$this->mg_module])
                ->update($data);
            $res ? json_response(1, '保存成功') : json_response(0, '保存失败');
        }else {
            $group_list = Db::name('mg_group')
                ->where(['mg_module'=>$this->mg_module])
                ->field('id, parent_id, group_name')
                ->select();
            $group_list = arr_tree($group_list);
            foreach ($group_list as &$item) {
                $item['group_name']   = str_repeat('&nbsp;', ($item['level'] - 1) * 8) . '|-' . $item['group_name'];
            }
            $access_list = Db::name('mg_menu')
                ->where('mg_module', $this->mg_module)
                ->field('id, parent_id, name')
                ->order('sort ASC, id DESC')
                ->select();
            $access_list = arr_tree($access_list, true);
            $data = Db::name('mg_group')
                ->where(['id'=>$id, 'mg_module'=>$this->mg_module])
                ->field('parent_id, group_name, access, status')
                ->find();
            $data['access'] = explode(',', $data['access']);
            $this->assign('group_list', $group_list);
            $this->assign('access_list', $access_list);
            $this->assign('data', $data);
            return $this->fetch('common@mg_group/add_group');
        }
    }

    /**
     * 删除管理组
     * date 2020/8/6 23:28
     */
    public function del_group() {
        $id = input('id');
        if($id == 1) json_response(0, '系统管理组不可删除');
        $have_children = Db::name('mg_member')
            ->where('group_id', $id)
            ->value('id');
        if($have_children) json_response(0, '管理组存在管理员不可删除');
        $res = Db::name('mg_group')
            ->where(['id'=>$id, 'mg_module'=>$this->mg_module])
            ->delete();
        $res ? json_response(1, '删除成功') : json_response(0,'删除失败');
    }

        /**
     * 错误日志记录
     * @return void
     * @author Sam
     * date 2021-09-07
     */
    public function log(){
        if(IS_POST) {
            $search = request()->param();
            $db   = Db::name('error_log')->order('numid desc');
            $list = $db->page($search['page'], $search['limit'])->select();
            admin_response(0, 'success', $list, $db->count());
        }

        return $this->render_table([
            "page"=>true,
             "cols" => [ 
                ["field"=>"url", "title"=>"链接","align"=>"center"],
                ["field"=>"weizhi", "title"=>"位置","align"=>"center"],
                ["field"=>"tishi", "title"=>"提示","align"=>"center"],
                ["field"=>"line", "title"=>"行数","align"=>"center"],
                ["field"=>"addtime", "title"=>"时间","align"=>"center"],
             ],
             
             ],false);
    }

    /**
     * 日志
     * @Author zengxin(573908667@qq.com)
     * @return mixed
     */
    public function mysql_log()
    {
        if (IS_POST) {
            $search = request()->param();
            $where  = [];
            $orderBy = '';

            if (isset($search['level']) && $search['level'] != '') $where[] = ['level', '=', $search['level']];
            if (isset($search['module']) && $search['module'] != '') $where[] = ['module', '=', $search['module']];
            if (isset($search['controller']) && $search['controller'] != '') $where[] = ['controller', '=', $search['controller']];
            if (isset($search['action']) && $search['action'] != '') $where[] = ['action', '=', $search['action']];

            if ((isset($search['field']) && $search['field'] != '') && (isset($search['order']) && $search['order'] != '')) $orderBy = $search['field'].' '.$search['order'];

            $db = SystemLog::where($where)->order($orderBy);

            $list = $db->limit(paginator())->select();
            foreach ($list as &$value){
                $value['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
            }

            admin_response(0, 'success', $list, $db->count());
        }

        return $this->render_table([
            "page"        => true,
            "cols"        => [
                ["field" => "id", "title" => "编号", "align" => "center","sort"=>true],
                ["field" => "level", "title" => "日志级别", "align" => "center"],
                ["field" => "module", "title" => "模块", "align" => "center"],
                ["field" => "controller", "title" => "控制器", "align" => "center"],
                ["field" => "action", "title" => "方法", "align" => "center"],
                ["field" => "request_data", "title" => "数据", "align" => "center"],
                ["field" => "msg", "title" => "信息", "align" => "center"],
                ["field" => "request_ip", "title" => "请求IP", "align" => "center"],
                ["field" => "create_time", "title" => "创建时间", "align" => "center"],
            ],
            "where"       => [
                'level' => ['type' => 'input', 'title' => '日志级别'],
                'module' => ['type' => 'input', 'title' => '模块'],
                'controller' => ['type' => 'input', 'title' => '控制器'],
                'action' => ['type' => 'input', 'title' => '方法'],
            ],
            'autoSort'=>1,
        ], false);
    }
}